package com.hyphenate.easeui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.easeui.R;
import com.paishen.common.util.BaseUtil;

/**
 * 聊天页面底部的聊天输入菜单栏 <br/>
 * 主要包含3个控件:EaseChatPrimaryMenu(主菜单栏，包含文字输入、发送等功能), <br/>
 * EaseChatExtendMenu(扩展栏，点击加号按钮出来的小宫格的菜单栏), <br/>
 * 以及EaseEmojiconMenu(表情栏)
 */
public class EaseChatGiftMenu extends LinearLayout {
    public EaseChatGiftMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EaseChatGiftMenu(Context context) {
        this(context, null);
    }

    private float balance;
    private TextView txBalance;

    private String giftLollipop, giftCake, giftWine;

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.ease_chat_menu_gift, this);

        giftLollipop = context.getString(R.string.gift_lollipop);
        giftCake = context.getString(R.string.gift_cake);
        giftWine = context.getString(R.string.gift_wine);

        findViewById(R.id.lay_gift_2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = 4;
                int value = 2;
                if (checkBalance(value)) {
                    // id(long) lillipop 恒定为4
                    if (callback != null) callback.onGiftClick(id, giftLollipop, value, v);
                }
            }
        });

        findViewById(R.id.lay_gift_5).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = 5;
                int value = 5;
                if (checkBalance(value)) {
                    // id(long) cake 恒定为5
                    if (callback != null) callback.onGiftClick(id, giftCake, value, v);
                }

            }
        });

        findViewById(R.id.lay_gift_10).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = 6;
                int value = 10;
                if (checkBalance(value)) {
                    // id(long) wine 恒定为6
                    if (callback != null) callback.onGiftClick(id, giftWine, value, v);
                }
            }
        });

        findViewById(R.id.tx_charge).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) callback.onChargeClick(v);
            }
        });

        txBalance = (TextView) findViewById(R.id.tx_balance);
    }

    private boolean checkBalance(int changed) {
        if (balance - changed < 0) {
            BaseUtil.showToast(R.string.gift_overdraft);
            return false;
        }
        return true;
    }

    public void setBalance(float balance) {
        if (balance < 0) balance = 0;
        this.balance = balance;
        txBalance.setText(getContext().getString(R.string.gift_balance, (int) balance));
    }

    private EaseChatGiftMenuItemClickListener callback;

    public void setEaseChatGiftMenuItemClickListener(EaseChatGiftMenuItemClickListener listener) {
        this.callback = listener;
    }


    public interface EaseChatGiftMenuItemClickListener {
        void onGiftClick(long giftId, String name, int value, View view);

        void onChargeClick(View view);
    }

}
